<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>
          <el-input
            style="width: 475px"
            placeholder="请输入内容"
            v-model="input4"
          >
            <i
              @click="lookUp"
              slot="suffix"
              class="el-input__icon el-icon-search"
            ></i>
          </el-input>
        </span>

        <el-button @click="dialogFormVisible = true" class="btn" type="primary"
          >添加用户</el-button
        >
      </div>
      <!-- 内容 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column width="50" type="index"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="create_time" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-switch @change="isStatus(row)" v-model="row.mg_state">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template #default="{ row }">
            <el-button
              style="width: 65px"
              type="primary"
              icon="el-icon-edit"
              @click="details(row)"
            ></el-button>
            <el-button
              @click="delUser(row)"
              style="width: 65px"
              type="danger"
              icon="el-icon-delete"
            ></el-button>
            <el-button
              @click="isRole(row)"
              style="width: 65px"
              type="warning"
              icon="el-icon-setting"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="2"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>

          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
        </el-form>
      </span>

      <span slot="footer" class="dialog-footer">
        <el-button @click="adduser('remove')">取 消</el-button>
        <el-button type="primary" @click="adduser('add')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户弹框 -->
    <el-dialog title="修改用户" :visible.sync="detailsUser">
      <el-form :rules="rulesTwo" :model="userDetails">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            :value="userDetails.username"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input
            v-model="userDetails.email"
            :value="userDetails.email"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="mobile"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="userDetails.mobile"
            :value="userDetails.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnModifyTwo">取 消</el-button>
        <el-button type="primary" @click="btnModify">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配角色 -->
    <el-dialog width="1075px" title="分配角色" :visible.sync="distribution">
    </el-dialog>
    <el-dialog title="分配角色" :visible.sync="distribution" width="1075px">
      <div>
        <p>
          当前用户 :
          {{ roleList.username }}
        </p>
        <p>
          当前角色 :
          {{ roleList.role_name }}
        </p>
        <p>
          分配新角色
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in roleListda"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="typeRoleTwo">取 消</el-button>
        <el-button type="primary" @click="btnRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserList,
  modifyStatus,
  addList,
  delList,
  detailsUser,
  modifyUser,
  roleUser,
  updateRole
} from '@/api/userList'
import { Message } from 'element-ui'
export default {
  name: 'my-users',
  data () {
    return {
      // 添加
      ruleForm: {
        username: '', // 用户名
        password: '', // 密码
        email: '', // 邮箱
        mobile: '' // 手机号
      },
      // 校验
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码'
          }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱'
          }
        ],
        mobile: [
          {
            required: true,
            message: '请输入手机号'
          }
        ]
      },
      rulesTwo: {
        email: [
          {
            required: true,
            message: '请输入邮箱'
          }
        ],
        mobile: [
          {
            required: true,
            message: '请输入手机号'
          }
        ]
      },
      roleList: {}, // 当前角色对象回显
      roleListda: [], // 角色列表
      value: '', // 分配下拉
      distribution: false, // 角色
      userDetails: {}, // 回显
      formLabelWidth: '120px', // 弹框大小
      dialogFormVisible: false, // 添加弹框显示隐藏
      detailsUser: false, // 修改弹框显示隐藏
      input4: '',
      currentPage4: 1, // 默认第几页
      total: 0, // 总数据几条
      tableData: [],
      myUserPage: {
        query: '',
        pagenum: 1, // 当前页
        pagesize: 2 // 每页
      }
    }
  },
  created () {
    // 获取用户列表
    this.myUser()
  },

  methods: {
    // 获取用户列表
    async myUser () {
      const res = await getUserList(this.myUserPage)
      console.log(res)
      // 共计多少条
      this.total = res.data.total
      this.tableData = res.data.users
    },
    // 分页
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.myUserPage.pagesize = val
      this.myUser()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      // 点击哪一页给到当前页
      this.myUserPage.pagenum = val
      this.myUser()
    },
    // 修改状态
    async isStatus (row) {
      this.tableData.id = row.id
      console.log(row)
      const res = await modifyStatus(row.id, row.mg_state)
      Message.success(res.meta.msg)
      console.log(res)
    },
    // 添加用户
    adduser (val) {
      if (val === 'add') {
        this.$refs.ruleForm.validate(async (val) => {
          console.log(val)
          if (!val) {
            // eslint-disable-next-line no-useless-return
            return
          } else {
            const res = await addList(this.ruleForm)
            console.log(res)
            this.myUser()
            Message.success('添加成功')
            this.$refs.ruleForm.resetFields()
            this.dialogFormVisible = false
          }
        })
      } else if (val === 'remove') {
        this.$refs.ruleForm.resetFields()
        this.dialogFormVisible = false
      }
    },
    // 查找
    async lookUp () {
      this.myUserPage.query = this.input4
      // console.log(this.myUserPage.query)
      const res = await getUserList(this.myUserPage)
      console.log(res.data.users)
      this.tableData = res.data.users
      this.input4 = ''
    },
    // 删除
    delUser (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delList(row.id)
          // console.log(res.meta.status)
          if (res.meta.status === 400) {
            Message.error(res.meta.msg)
          } else if (res.meta.status === 200) {
            this.myUser()
            Message.success(res.meta.msg)
          }
        })
        .catch(() => {
          Message.error('取消删除')
        })
    },
    // 个人信息回显
    async details (row) {
      // 打开弹窗
      this.detailsUser = true
      const res = await detailsUser(row.id)
      console.log(res.data)
      this.userDetails = res.data
    },
    // 修改数据
    async btnModify () {
      const resModify = await modifyUser(this.userDetails.id, this.userDetails)
      console.log(resModify)
      this.myUser()
      Message.success(resModify.meta.msg)
      // 关闭弹窗
      this.detailsUser = false
    },
    btnModifyTwo () {
      Message('取消修改')
      // 关闭弹窗
      this.detailsUser = false
    },
    // 点击获取角色列表
    async isRole (row) {
      // 弹框显示
      this.distribution = true
      // console.log(row)
      this.roleList = row
      // console.log(this.roleList)
      const res = await roleUser()
      console.log(res.data)
      this.roleListda = res.data
      // console.log(this.roleListda)
    },
    // 修改角色
    async btnRole () {
      // console.log(this.value)
      const res = await updateRole(this.roleList.id, {
        id: this.roleList.id,
        rid: this.value,
        username: this.roleList.username,
        mobile: this.roleList.mobile,
        email: this.roleList.email
      })
      // console.log(res.meta.status)
      // console.log(res.meta.msg)
      if (this.value === '') {
        Message.warning('请选择角色')
      } else {
        if (res.meta.status === 200) {
          this.myUser()
          Message.success(res.meta.msg)
          this.distribution = false
          this.value = ''
        } else if (res.meta.status === 400) {
          Message.error(res.meta.msg)
          this.distribution = false
          this.value = ''
        }
      }
    },
    // 取消修改角色
    typeRoleTwo () {
      this.distribution = false
      this.value = ''
      Message('取消修改')
    }
  }
}
</script>

<style scoped lang="scss">
.block {
  margin-top: 15px;
}
.box-card {
  margin-top: 15px;
}
.el-input__icon {
  width: 50px;
  height: 38px;
  background-color: #f5f7fa;
  border: 1px solid #dcdfe6;
  margin-right: -58px;
  border-radius: 2px;
}
.btn {
  margin-left: 70px;
}
</style>
